all files / lib/ index.js

98.72% Statements 77/78
100% Branches 36/36
100% Functions 14/14
98.72% Lines 77/78
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147              14×       21× 21× 21×   21× 21×   21×     17×     16× 16× 11× 11× 11×   16×                                                                                                          
/**
 * Created on 2017/8/15.
 * @fileoverview 请填写简要的文件说明.
 * @author joc (Chen Wen)
 */
const getUrl = function (version, key) {
    return `http://webapi.amap.com/maps?v=${version}&key=${key}`;
};
 
const checkExists = function (el, err) {
    if (!el) {
        throw new Error(err);
    }
};
 
const getElementById = function (id, options) {
    options = options || {};
    let parent = options.parent || document.body;
    if (typeof parent === 'string') {
        parent = getElementById(parent);
    }
    const searchIn = options.searchIn || parent;
    const elType = options.elType || 'div';
 
    if (!id) {
        return null;
    }
 
    if (id instanceof Element && !id.parentElement) {
        parent.appendChild(id);
        return id;
    }
 
    let el = searchIn.querySelector(`#${id}`);
    if (!el) {
        el = document.createElement(elType);
        el.id = id;
        parent.appendChild(el);
    }
    return el;
};
 
const AMapSource = function (options) {
    const self = this;
    checkExists(options && typeof options === 'object', 'Expect an object argument: `options`.');
 
    self.panelEl = getElementById(options.panelEl, {elType: options.panelType});
 
    options = Object.assign({
        version: '1.3',
        scriptLoaded: false,
        scriptPanel: self.panelEl,
        mapOptions: {},
        containerConfig: {},
        scriptId: 'script'
    }, options);
 
    const version = options.version;
    const key = options.key;
    checkExists(key, 'Expect a `key` property in `options`.');
    checkExists(options.panelEl, 'Expect a `panelEl` property in `options`.');
 
    self.url = getUrl(version, key);
    self.setLoaded(options.scriptLoaded);
    self.version = version;
    self.key = key;
 
    self.mapOptions = options.mapOptions;
    self._pendingCallbacks = [];
    self.container = self._getContainer(options.containerConfig);
    self._initCreate(options);
 
    self.postCreated = options.postCreated;
    self.lazy = !!options.lazy;
    self.lazy || self.render();
};
 
AMapSource.prototype._getContainer = function (config) {
    checkExists(config && typeof config === 'object', 'expect an object argument: `config`');
    return getElementById(config.id || this.panelEl.id, Object.assign({
        parent: this.panelEl
    }, config));
};
 
AMapSource.prototype.setLoaded = function (flg) {
    if (!this.scriptLoaded) {
        this.scriptLoaded = flg;
    }
};
 
AMapSource.prototype.loadScript = function () {
    const self = this;
 
    self.setLoaded(false);
 
    const script = self.scriptEl = getElementById(self.scriptId, {elType: 'script', parent: self.scriptPanel});
 
    return new Promise(function (resolve) {
        const onload = function () {
            resolve.apply(this, AMap);
            self.setLoaded(true);
        };
 
        if (self.scriptLoaded) {
            return onload();
        }
 
        script.src = self.url;
        script.onload = onload;
    });
};
 
AMapSource.prototype._create = function (AMap) {
    return new AMap.Map(this.container.id, Object.assign({
        resizeEnable: true,
        zoom: 10
    }, this.mapOptions));
};
 
AMapSource.prototype._initCreate = function (options) {
    const self = this;
    let create = options.create;
    if (!create) {
        create = self._create;
    }
 
    self.create = function () {
        const r = create.call(self, AMap);
        self.postCreated && self.postCreated(r);
        return r;
    };
};
 
AMapSource.prototype.render = function () {
    const self = this;
    return self.loadScript().then(function (AMap) {
        self.create(AMap);
    });
};
 
Object.assign(AMapSource, {
    _checkExists: checkExists,
    _getElementById: getElementById
});
 
module.exports = AMapSource;